﻿using BwTMusicHelper;
using BwTMusicModels;
using HwjUIDI.Utility.DatabaseHelper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BwTMusicServices.Album
{
    public class AlbumServices
    {

        public North_Album GetAlbumInfo(string id)
        {
            North_Album album = new North_Album();
            try
            {
                string sql = $"select * from North_Album where AlbumId='{id}'";
                using (SqlDataReader dr = DatabaseFactory.GetDatabase().ExecuteDataReaderSQL(sql))
                {
                    while (dr.Read())
                    {
                        album = new ModelsHelper().GetNorthAlbumModels(dr);
                    }
                }
            }
            catch (Exception)
            {

            }
            return album;
        }

        public List<MusicHelper> GetAllNorthAlbumMusic(int CurrentPage, int albumId, out int total)
        {
            List<MusicHelper> Lists = new List<MusicHelper>();
            total = 0;
            try
            {
                if (CurrentPage > 0)
                {
                    StringBuilder sbSQL = new StringBuilder();
                    sbSQL.Append("select top 15");
                    sbSQL.Append($" * from (select count(1) over() as totalnumber, ROW_NUMBER() over(order by A.MusicId) as rownumber,A.MusicId ,A.MusicName,B.SingerName,A.MusicSinger,A.MusicImgUrl,A.MusicBitRate,A.MusicLong from North_Music A left join North_Singer B on A.MusicSinger=B.SingerId  where A.IsVaild= 'Y' and A.MusicAlbum= '{albumId}' ");
                    sbSQL.Append(") WTX where rownumber > (");
                    sbSQL.Append((CurrentPage - 1) * 15);
                    sbSQL.Append(")");
                    using (SqlDataReader dr = DatabaseFactory.GetDatabase().ExecuteDataReaderSQL(sbSQL.ToString()))
                    {
                        while (dr.Read())
                        {
                            int iIndex = 0;
                            MusicHelper list = new MusicHelper();
                            list.Musics = new ModelsHelper().GetNorthMusicModels(dr);
                            list.Singer = new ModelsHelper().GetNorthSingerModels(dr);
                            list.Albums = new ModelsHelper().GetNorthAlbumModels(dr);
                            Lists.Add(list);
                            if (iIndex == 0)
                            {
                                total = DatabaseFactory.GetDatabase().GetInt(dr, "totalnumber");
                            }
                            iIndex++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return Lists;
        }

    }
}
